package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/delete-node-in-a-bst/description/
 * @create 2025/9/28 19:01
 * @since JDK17
 */

public class Solution82 {
    TreeNode f(TreeNode root) {
        if (root.right == null) {
            return root;
        }
        return f(root.right);
    }


    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) {
            return null;
        }
        if (root.val > key) {   //
            root.left = deleteNode(root.left, key);
        } else if (root.val < key) {
            root.right = deleteNode(root.right, key);
        } else {    // 找到了
            if (root.left == null && root.right == null) {
                return null;
            } else if (root.left == null) {
                return root.right;
            } else if (root.right == null) {
                return root.left;
            } else {
                var right = f(root.left);
                right.right = root.right;
                return root.left;
            }
        }
        return root;
    }
}
